public member function
<vector>
swap containers (1) | void swap (vector& x);
|
---|
swap elements (2) | static void swap (reference ref1, reference ref2);
|
---|
swap containers (1) | void swap (vector& x);
|
---|
swap elements (2) | static void swap (reference ref1, reference ref2) noexcept; |
---|
Swap containers or elements
Parameters
- x
- Another vector<bool> container. Sizes may differ.
- ref1, ref2
- References to elements.
reference is a member type that accesses individual elements while providing an interface that simulates a reference to bool (see reference for more info).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
// vector<bool>::swap
#include <iostream>
#include <vector>
int main ()
{
std::vector<bool> foo;
std::vector<bool> bar;
foo.push_back(false);
foo.push_back(true);
foo.push_back(false);
bar.push_back(true);
bar.push_back(false);
foo.swap (foo[0], foo[1]);
bar.swap (bar.front(), bar.back());
foo.swap(bar);
std::cout << std::boolalpha;
std::cout << "foo contains:";
for (unsigned i=0; i<foo.size(); i++) std::cout << ' ' << foo[i];
std::cout << "\nbar contains:";
for (unsigned i=0; i<bar.size(); i++) std::cout << ' ' << bar[i];
std::cout << '\n';
return 0;
}
| |
Output:
foo contains: false true
bar contains: true false false
|
Data races
For (1), both containers are modified.
For (2), elements are modified: in bool vectors there are no guarantees on whether concurrently accessing other elements is safe.
Exception safety
For (1), see vector::swap.
For (2), it never throws exceptions (no-throw guarantee).